class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        const int INF = 0x3f3f3f3f;
        int m = prices.size();
        vector<vector<int>> f(m, vector<int>(3, -INF));
        auto g = f;
        f[0][0] = -prices[0];
        g[0][0] = 0;

        for (int i = 1; i < m; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                f[i][j] = max(f[i - 1][j], g[i - 1][j] - prices[i]);
                g[i][j] = g[i - 1][j];
                if (j - 1 >= 0) g[i][j] = max(g[i - 1][j], f[i - 1][j - 1] + prices[i]);
            }
        }

        return max(g[m - 1][0], max(g[m - 1][1], g[m - 1][2]));
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee)
    {
        int m = prices.size();
        vector<int> f(m), g(m);
        f[0] = -prices[0];
        for (int i = 1; i < m; i++)
        {
            f[i] = max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = max(g[i - 1], f[i - 1] + prices[i] - fee);
        }
        return max(f[m - 1], g[m - 1]);
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        int m = prices.size();
        vector<vector<int>> dp(m, vector<int>(3));
        dp[0][0] = -prices[0];
        for (int i = 1; i < m; i++)
        {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
            dp[i][1] = dp[i - 1][0] + prices[i];
            dp[i][2] = max(dp[i - 1][2], dp[i - 1][1]);
        }
        return max(dp[m - 1][1], dp[m - 1][2]);
    }
};
class Solution {
public:
    int deleteAndEarn(vector<int>& nums)
    {
        const int N = 10001;
        int arr[N] = { 0 };
        for (auto e : nums) arr[e] += e;

        vector<int> f(N), g(N);
        f[0] = arr[0];
        for (int i = 1; i < N; i++)
        {
            f[i] = g[i - 1] + arr[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[N - 1], g[N - 1]);
    }
};